<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/5
 * Time: 19:55
 */
namespace App\Http\Controllers\Admins;

use App\Http\Controllers\Controller;
use App\Models\SysModel;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Cookie;
use Symfony\Component\HttpFoundation\Cookie as Cookies;




class SysController extends Controller
{


    public function loginFrom(Request $request)
    {

        $param = $request->all();
        if(isset($param['back']))
        {//一次性Session数据
            $request->session()->flash('back', $param['back']);
        }
        return view("admins/sys/loginFrom");
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function login(Request $request)
    {
        $rules = [
            'username' => 'required|max:10',
            'password' => 'required',
        ];
        $message = [
            'username.required'=>'账号不能为空！',
            'username.max'=>'账号不能大于10位！',
            'password.required'=>'密码不能为空！',
            // 'code.required'=>'验证码不能为空！',
            // 'code.between'=>'验证码必须为4位！',
        ];
        $this->validate($request,$rules,$message);

        $postData = $request->all();
        $username = $postData['username'];
        $password = $postData['password'];
        $sys = new \App\Models\SysModel();

        //back有值,要跳转
        $back = session('back');
        $successUrl = "";
        $loginUrl = url()->previous();
        if($back!='' || $back != null)
        {
            $successUrl = $back;
            $loginUrl= $this->getRedirectUrl();

        }
        $result = $sys->getAdmin($username);
        if($result)
        {

            if($result['passwords']==md5($password))
            {
                //cookie写入
                $cookie= Cookie::queue('username',$username, 60); //60分钟
                //Cookie::queue('username', null , -1); //销毁
                if($back=='' || $back == null)
                {
                    return redirect("admins/main");
                }else{
                    //登录成功进行跳转(自定义函数)
                    redirectUrl($successUrl);
                }
            }else{

                $this->loginError($loginUrl,"登陆失败！",$back);
            }
        }
        else
        {
            //exit('跳转错误提示网页');
            //$msg = ['error'=>"登陆失败！"];
            //return view("admins/sys/loginFrom",$msg);
            $this->loginError($loginUrl,"无此用户！",$back);

        }

    }
    /*$loginUrl 登陆网址
     * $msg 错误信息
     * $back 登陆成功跳转网址
     */
    public function loginError($loginUrl,$msg,$back)
    {
        $back = "&back=".$back;
        $loginUrl = explode('?',$loginUrl);
        $loginUrl = $loginUrl[0];
        redirectUrl($loginUrl."?error=".$msg.$back);
    }
    public function loginOut()
    {
        Cookie::queue('username', null , -1); //销毁
        return redirect("/admins/main");
        //return view("admins/sys/loginOut");

    }
    public function forgotPassword()
    {
        return view("admins/sys/forgotPassword");

    }
    public function welcome()
    {
        return view("admins/welcome");
    }
    public function main()
    {
        return view("admins/main");
    }
}
